const  { app,BrowserWindow } = require('electron')
const  WinState = require('electron-win-state').default
const path = require('path')


//获取网站截图
require('./controller/getSource')
require('./controller/alert')

//open window
require('./controller/openWindow')

require('./controller/getFileList')


//buildMenu
require('./controller/buildMenu')

//托盘
const createTray = require('./controller/tray')


const createWindow = () => {

    //实例化窗口管理工具（1/3）
    const winState = new WinState({
        defaultWidth:1000,
        defaultHeight:800,
        electronStoreOptions:{
            name:"electron-state-main"
        }
    })

    const win = new BrowserWindow({
        //装载窗口管理工具(2/3)
        //自定义窗口状态
        ...winState.winOptions,
        webPreferences:{
            //用绝对路径引入preload渲染进程
            //定义预加载的js
            preload:path.resolve(__dirname,'./preload/index.js')
        },
        // 不显示浏览器窗口(与ready-to-show作配合)
        show:false
    })

    //引入地址
    win.loadURL('http://localhost:5173')



    //打开开发者工具
    // win.webContents.openDevTools()

    //指定窗口管理工具管理的浏览器窗口(3/3)
    winState.manage(win)

    //当资源加载完毕准备显示
    win.on('ready-to-show',() => {
        win.show()
    })


    
    createTray(app,win)


}


//当electron准备完毕
app.whenReady().then(() => {
    createWindow()


    //监听点击图标事件，创建浏览器窗口（默认执行）
    app.on('activate',() => {
        if(BrowserWindow.getAllWindows.length === 0) {
            createWindow()
        }
    })


    

    //监听浏览器窗口全部关闭事件
    app.on('window-all-closed',() => {
        if(process.platform !== 'darwin') {
            app.quit()
        }
    })

    

    

})


